﻿using System.Collections.Generic;
using Wedn.Core.Common;
using Wedn.Core.Entity;

namespace Wedn.Core.Service
{
    /// <summary>
    /// 关系管理类
    /// </summary>
    public class RelationManager
    {
        private Wedn.Interface.DAL.IRelation dao;
        /// <summary>
        /// 链接管理类构造函数
        /// </summary>
        /// <exception cref="未找到系统数据库访问组件"></exception>
        public RelationManager()
        {
            dao = Wedn.Interface.DAOFactory.CreateRelationDAOInstance();
            if (dao == null)
            {
                throw new System.Exception("缺失数据库访问组件!");
            }
        }

        /// <summary>
        /// 向关系表中插入一行新数据, 返回执行结果受影响行数
        /// </summary>
        /// <param name="relation">要插入的对象</param>
        /// <returns>执行结果受影响行数</returns>
        public bool Insert(Relation relation)
        {
            bool res = dao.Insert(relation) > 0;
            TermManager termManager = new TermManager();
            termManager.UpdateCount(relation.TermId);
            return res;
        }

        /// <summary>
        /// 为一个对象向关系表中批量插入新数据, 返回执行结果受影响行数
        /// </summary>
        /// <param name="objectId">要插入的对象ID</param>
        /// <param name="objectType">要插入的对象类型</param>
        /// <param name="termsId">关联标签ID</param>
        /// <returns>返回执行结果受影响行数</returns>
        public int Insert(int objectId, ObjectType objectType, params int[] termsId)
        {
            int res = 0;
            foreach (var termId in termsId)
            {
                res += dao.Insert(new Relation() { ObjectId = objectId, ObjectType = (int)objectType, TermId = termId });
            }
            //重新统计;
            new TermManager().UpdateCount();
            return res;
        }

        /// <summary>
        /// 根据关系ID删除对应的关系记录, 返回执行结果受影响行数
        /// </summary>
        /// <param name="id">要删除记录的ID</param>
        /// <returns>执行结果受影响行数</returns>
        public bool Delete(int id)
        {
            bool res = dao.Delete(id) > 0;
            new TermManager().UpdateCount();
            return res;
        }

        /// <summary>
        /// 根据关系ID数组删除对应的关系记录, 返回执行结果受影响行数
        /// </summary>
        /// <param name="ids">要删除记录的ID数组</param>
        /// <returns>执行结果受影响行数</returns>
        public bool Delete(int[] ids)
        {
            bool res = dao.Delete(ids) > 0;
            new TermManager().UpdateCount();
            return res;
        }

        /// <summary>
        /// 根据关系对象删除一个其所有关系, 返回执行结果受影响行数
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <returns>执行结果受影响行数</returns>
        public bool Delete(int objectId, ObjectType objectType)
        {
            bool res = dao.Delete(objectId, objectType.ToString().ToLower()) > 0;
            new TermManager().UpdateCount();
            return res;
        }

        /// <summary>
        /// 更新一个完整的关系对象, 返回执行结果受影响行数
        /// </summary>
        /// <param name="relation">要更新的对象</param>
        /// <returns>执行结果受影响行数</returns>
        public bool UpdateRelation(Relation relation)
        {
            bool res = dao.UpdateRelation(relation) > 0;
            new TermManager().UpdateCount();
            return res;
        }

        /// <summary>
        /// 更新一个关系信息, 返回执行结果受影响行数
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <param name="oldTermId">原先的标签ID</param>
        /// <param name="newTermId">更新的标签ID</param>
        /// <returns>执行结果受影响行数</returns>
        public bool UpdateInfo(int objectId, ObjectType objectType, int oldTermId, int newTermId)
        {
            bool res = dao.UpdateInfo(objectId, objectType.ToString().ToLower(), oldTermId, newTermId) > 0;
            new TermManager().UpdateCount();
            return res;
        }

        /// <summary>
        /// 将一个标签ID全部更新为新的标签ID, 返回执行结果受影响行数
        /// </summary>
        /// <param name="oldTermId">原先的标签ID</param>
        /// <param name="newTermId">更新的标签ID</param>
        /// <returns>执行结果受影响行数</returns>
        public bool UpdateTermId(int oldTermId, int newTermId)
        {
            bool res = dao.UpdateTermId(oldTermId, newTermId) > 0;
            new TermManager().UpdateCount();
            return res;
        }

        /// <summary>
        /// 获取表中主键最大值, 返回主键最大值
        /// </summary>
        /// <returns>主键最大值</returns>
        public int GetMaxId()
        {
            return dao.SelectMaxId();
        }

        /// <summary>
        /// 根据标签ID和对象类型取出当前关系表中对应的关系对象ID, 不存在则返回空数组
        /// </summary>
        /// <param name="objectType">关系对象类型</param>
        /// <param name="termId">标签ID</param>
        /// <returns>指定标签指定类型的全部关系对象ID数组</returns>
        public int[] GetObjectId(ObjectType objectType, int termId)
        {
            int[] ns = dao.SelectObjectId(objectType.ToString().ToLower(), termId);
            if (ns == null)
                ns = new int[0];
            return ns;
        }

        /// <summary>
        /// 根据对象ID和对象类型取出当前关系表中对应的标签ID, 不存在则返回空数组
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <returns>指定对象的全部标签ID数组</returns>
        public int[] GetTermId(int objectId, ObjectType objectType)
        {
            int[] ns = dao.SelectTermId(objectId, objectType.ToString().ToLower());
            if (ns == null)
                ns = new int[0];
            return ns;
        }

        /// <summary>
        /// 取出当前关系表中的所有关系信息集合, 不存在则返回空数组
        /// </summary>
        /// <returns>所有关系信息集合</returns>
        public List<Relation> GetRelationList()
        {
            List<Relation> list = dao.SelectRelationList();
            if (list == null)
                list = new List<Relation>();
            return list;
        }

        /// <summary>
        /// 根据对象ID和对象类型取出当前关系表中的所有关系信息集合, 不存在则返回空数组
        /// </summary>
        /// <param name="objectId">关系对象ID</param>
        /// <param name="objectType">关系对象类型</param>
        /// <returns>包涵指定对象关系信息集合</returns>
        public List<Relation> GetRelationList(int objectId, ObjectType objectType)
        {
            List<Relation> list = dao.SelectRelationList(objectId, objectType.ToString().ToLower());
            if (list == null)
                list = new List<Relation>();
            return list;
        }

        /// <summary>
        /// 根据关系ID取出当前关系表中的关系信息对象, 不存在则返回null
        /// </summary>
        /// <param name="id">关系ID</param>
        /// <returns>关系信息对象</returns>
        public Relation GetRelation(int id)
        {
            return dao.SelectRelation(id);
        }
    }
}
